package cn.seapon.talker.fragments;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import cn.seapon.talker.ChatActivity;
import cn.seapon.talker.R;
import cn.seapon.talker.adapters.MessageListAdapter;
import cn.seapon.talker.bean.MessageEntity;
import cn.seapon.talker.common.FriendListInfomation.FriendInfomation;
import cn.seapon.talker.infocarrier.MessageAssortment;
import cn.seapon.talker.infocarrier.SharedItems;
import cn.seapon.talker.ui.SwipeListView;

/**
 * @author 作者 E-mail:
 * @version 创建时间：2015年1月1日 下午8:37:23 类说明
 */
public class MessageFragment extends TalkerBasedFragment {
	private static final String TAG = "消息";
	private Context context;
	

	private List<MessageAssortment> data ;
	private SwipeListView mListView;

	private View contentView;

	public MessageFragment(Context context, String content) {
		super(context, content);
		this.content = content;
		this.context = context;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		contentView = inflater.inflate(R.layout.fragment_message, container,
				false);
		mListView = (SwipeListView) contentView.findViewById(R.id.mListView);
		initData();
		initView();
		return contentView;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void onStop() { 
		super.onStop();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onResume() {
//		toastShow(TAG);
		initData();
		initView();
		super.onResume();
	}

	protected void initView() {
		final MessageListAdapter mAdapter = new MessageListAdapter(context,
				data);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				MessageAssortment assortment = data.get(position);
//				Toast.makeText(context, assortment.getFriendId(), Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(context,ChatActivity.class);
				intent.putExtra("friendEntity", SharedItems.friendListInfomation.getInfoById(assortment.getFriendId()));
				startActivity(intent);
			}
		});
		mListView.setAdapter(mAdapter);
		mAdapter.notifyDataSetChanged();
	}

	/**
	 * 批量添加数据
	 */
	protected void initData() {
		data = new ArrayList<MessageAssortment>();
		int friendListCount = SharedItems.friendListInfomation.getCount();
		for (int i = 0; i < friendListCount; i++) {
			FriendInfomation infomation = SharedItems.friendListInfomation
					.getAt(i);
			String friendid = infomation.getId();
			MessageEntity entity;
			ArrayList<MessageEntity> historys = SharedItems.messageEntityDao
					.getAllRecord(SharedItems.userInformation.getAccount(),
							friendid);
			// ChatDatabaseUtils.getTheHistory(context,friendid,1);
			if (historys.size() > 0) {// 有记录
				entity = historys.get(0);
				String title = getMATitle(infomation);
				String time = getMATime(entity);
				MessageAssortment msg = new MessageAssortment(title,
						entity.getContent(), time,
						SharedItems.messageEntityDao.getUnreadNum(
								SharedItems.userInformation.getAccount(),
								friendid));
				msg.setFriendId(friendid);
				msg.setIcon_id(R.drawable.qq_icon);
				data.add(msg);
			}
		}
		
	}

	private String getMATime(MessageEntity entity) {
		DateFormat format = new SimpleDateFormat("dd");
		Date date = new Date(entity.getPosttime());

		// 获得今天的日期 和 发送日期
		String thisDay = format.format(new Date());
		String sendDay = format.format(date);
		if (thisDay.equals(sendDay)) {
			format = new SimpleDateFormat("hh:mm");// 小时分制
		} else {
			format = new SimpleDateFormat("MM/dd");
		}
		return format.format(date);
	}

	private String getMATitle(FriendInfomation infomation) {
		String title = "";
		String id = infomation.getId();
		String nickname = infomation.getNickname();
		String remarkName = infomation.getRemarkname();
		if (TextUtils.isEmpty(remarkName)) {
			if (TextUtils.isEmpty(nickname)) {
				title = id;
			} else {
				title = nickname + "(" + id + ")";
			}
		} else {
			title = remarkName + "(" + id + ")";
		}
		return title;
	}


	
}
